package bookoperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-05-25
 * Time: 16:04
 */
public class DropOperation implements IBookOperations{
    @Override
    public void work(BookList booklist) {
        System.out.println("请输入要删除的图书名称");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int i = 0;
        int pos = -1;
        int size = booklist.getUseSize();
        for (; i < size; i++) {//判断图书是否存在
            if(booklist.getBook(i).getName().equals(name)){
                pos = i;
                break;
            }
        }
        if(i == booklist.getUseSize()){
            System.out.println("该图书不存在！！！");
            return ;
        }

        for (int j = pos; j < booklist.getBooksLength() - 1; j++) {
            Book book = booklist.getBook(j + 1);
            booklist.setBook(j,book);
        }
        booklist.setUseSize(size - 1);
        System.out.println("删除图书成功！！！");
    }
}
